package handler

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"log"
	"net/http"
	"video/globals"
	"video/internal/model"
	"video/internal/req"
	"video/internal/resp"
	"video/internal/service"
	"video/pkg"
)

type VideoHandler struct {
	VideoService  service.VideoServiceImpl
	ReqVideoList  req.ReqVideoList
	VideoListResp resp.VideoListResp
}

func NewVideoHandler() *VideoHandler {
	return &VideoHandler{
		VideoService:  service.NewVideoService(),
		ReqVideoList:  req.ReqVideoList{},
		VideoListResp: resp.VideoListResp{},
	}
}

func UploadVideo(c *gin.Context) {
	userId := c.GetUint("userI")
	var data model.User

	globals.DB.Where("id=?", userId).Find(&data).Limit(1)

	if data.CType != 2 {
		c.JSON(http.StatusOK, gin.H{
			"code":    10000,
			"message": "抱歉您没有权限",
			"data":    nil,
		})
		return
	}
	file, _ := c.FormFile("file")
	log.Println(file.Filename)

	dst := "./" + file.Filename
	// 上传文件至指定的完整文件路径
	c.SaveUploadedFile(file, dst)

	if !pkg.Uplocad(dst) {
		c.JSON(http.StatusOK, gin.H{
			"code":    10000,
			"message": "上传文件失败",
			"data":    nil,
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code":    http.StatusOK,
		"message": "上传文件成功",
		"data":    dst,
	})
}

func (v VideoHandler) VideoList(c *gin.Context) {

	if err := c.ShouldBind(&v.ReqVideoList); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code":    10000,
			"message": "参数获取失败",
			"data":    nil,
		})
		return
	}
	fmt.Println(&v.ReqVideoList)
	list, err := v.VideoService.VideoList(&v.ReqVideoList)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code":    10000,
			"message": "视频列表展示失败",
			"data":    err.Error(),
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code":    http.StatusOK,
		"message": "视频列表展示成功",
		"data":    list,
	})
}
